jQuery(document).ready(function() {
    /*
        Fullscreen background
    */
    $.backstretch("../assets/img/backgrounds/1.jpg");
    
    /*
        Form validation
    */
    $('.login-form input[type="text"], .login-form input[type="password"], .login-form textarea').on('focus', function() {
    	$(this).removeClass('input-error');
    });
    
    $('.login-form').on('submit', function(e) {
    	
    	$(this).find('input[type="text"], input[type="password"], textarea').each(function(){
    		if( $(this).val() == "" ) {
    			e.preventDefault();
    			$(this).addClass('input-error');
    		}
    		else {
    			$(this).removeClass('input-error');
    		}
    	});
    });

    $("#login-button").click(function(){
		var username = $("#form-username").val();
		var password = $("#form-password").val();
		var code = $("#code").val();
		if(username.length == 0||password.length==0){
			alert("用户名和密码不能为空!");
			return;
		}
		if(code.length == 0){
			alert("验证码填写不正确!")
			return;
		}
		$.ajax({
			url:'/studentManageSystem/checkLogin',
			data:{username:username,password:password,code:code},
			type:"post",
			success:function(data){
				if(data==""){
					$("#checkCode").attr('src','/studentManageSystem/checkCode?'+ Math.random());
					alert("用户名或者密码错误，请重试!");
				}else{
                    $(location).prop('href','/studentManageSystem/goToStarter');
				}
			}
		})
	})
});
